#pragma once
#include "miscellaneous.h"
#include <GL/glew.h>
#include <vector>

typedef void (*UpdateCallback)(char* buf, int w, int h, int bpp);

class GLCanvas
{
public:
    GLCanvas();
    ~GLCanvas();

    void init(int w, int h, int num_pbo = 2);

    void release();

    void render();

    void resize(int w, int h);

    inline int width() const { return _winW; }
    inline int height() const { return _winH; }
    inline int bytesPerPixel() const { return BytesPerPixel; }

    inline void dirty() { _dirty = true; }

    std::vector<unsigned char>& pixels() { return _data; }
    const std::vector<unsigned char>& pixels() const { return _data; }

protected:

    const GLenum TexTarget = GL_TEXTURE_RECTANGLE;
    const GLenum TexFormat = GL_RGBA;
    const GLuint BytesPerPixel = 4;

    bool _valid = false;
    bool _dirty = false;

    GLint _winW = 800;
    GLint _winH = 600;
    GLuint _vbo = 0;
    GLuint _vao = 0;
    GLuint _tid = 0;
    GLuint _program = 0;
    GLuint _pboIndex = 0;
    std::vector<GLuint> _pbo;
    std::vector<unsigned char> _data;

};

